package xyz.ljcc.ccmt.handler.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import xyz.ljcc.ccmt.model.base.Role;
import xyz.ljcc.ccmt.model.result.ResultModel;
import xyz.ljcc.ccmt.service.api.admin.RoleService;


/**
 * 角色请求头部分
 * 
 * @author liucan
 *
 */
@RestController
@RequestMapping("/admin/role")
public class RoleHandler {

	@Autowired
	private RoleService roleService;

	@RequestMapping(method = RequestMethod.POST)
	public ResultModel saveRole(Role role) {
		roleService.saveRole(role);
		return new ResultModel(true, null, "保存角色成功");
	}

	@RequestMapping(method = RequestMethod.PUT)
	public ResultModel editRole(Role role) {
		roleService.editRole(role);
		return new ResultModel(true, null, "更新角色成功");
	}

	@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
	public ResultModel delRole(@PathVariable(value = "id") Integer id) {
		roleService.delRole(id);
		return new ResultModel(true, null, "删除角色成功");
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultModel getRole(@PathVariable(value = "id") Integer id) {
		Role role = roleService.getRole(id);
		return new ResultModel(true, role, "单角色获取成功");
	}

	@RequestMapping(value = "/active", method = RequestMethod.POST)
	public ResultModel activeRole(Integer id, Integer active) {
		roleService.activeRole(id, active);
		return new ResultModel(true, null, active == 1 ? "启用成功" : "停用成功");
	}

	@RequestMapping(value = "loadRoleList", method = RequestMethod.POST)
	public ResultModel loadRoleList(String roleName) {
		List<Role> roles = roleService.loadRoleList(roleName);
		return new ResultModel(true, roles, "获取角色列表成功");
	}
	
	
	@RequestMapping(value="/permission/{roleId}",method=RequestMethod.GET)
	public ResultModel getPermission(@PathVariable(value="roleId") Integer roleId) {
		Map<String, Object> map = roleService.getPermission(roleId);
		return new ResultModel(true, map, "获取角色权限成功");
	}
	
	@RequestMapping(value="/permission",method=RequestMethod.POST)
	public ResultModel savePermission(Integer roleId,String modules) {
		roleService.savePermission(roleId, modules);
		return new ResultModel(true, "分配成功");
	}

}
